package zozopo.operations.oa;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.business.IPReportTB;
import zozopo.operations.comm.OperatingService;

public class AddressbookOS extends OperatingService{
	private static final Log logger = LogFactory.getLog(AddressbookOS.class);
	
	public AddressbookOS(Connection conn){
		super(conn);
	}
	
	public AddressbookOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<AddressbookTB> selectAddressbookBySql(String sql){
		List<AddressbookTB> addressbookList = null;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					addressbookList = new ArrayList<AddressbookTB>();
					while(resultSet.next()){
						AddressbookTB addressbook = new AddressbookTB();
						
						addressbook.setId_addressbook(resultSet.getInt(AddressbookTB.ID_ADDRESSBOOD));
						
						addressbook.setName_addressbook(resultSet.getString(AddressbookTB.NAME_ADDRESSBOOD));
						addressbook.setCompany_addressbook(resultSet.getString(AddressbookTB.COMPANY_ADDRESSBOOD));
						addressbook.setDepartments_addressbook(resultSet.getString(AddressbookTB.DEPARTMENTS_ADDRESSBOOD));
						addressbook.setDes_addressbook(resultSet.getString(AddressbookTB.DES_ADDRESSBOOD));
						addressbook.setDuties_addressbook(resultSet.getString(AddressbookTB.DUTIES_ADDRESSBOOD));
						addressbook.setJobs_addressbook(resultSet.getString(AddressbookTB.JOBS_ADDRESSBOOD));
						addressbook.setKind_addressbook(resultSet.getString(AddressbookTB.KIND_ADDRESSBOOD));
						addressbook.setLable_addressbook(resultSet.getString(AddressbookTB.LABLE_ADDRESSBOOD));
						addressbook.setMobile_addressbook(resultSet.getString(AddressbookTB.MOBILE_ADDRESSBOOD));
						addressbook.setPhone_addressbook(resultSet.getString(AddressbookTB.PHONE_ADDRESSBOOD));
						addressbook.setQq_addressbook(resultSet.getString(AddressbookTB.QQ_ADDRESSBOOD));
						addressbook.setStatus_addressbook(resultSet.getString(AddressbookTB.STATUS_ADDRESSBOOD));
						addressbook.setAddress_addressbook(resultSet.getString(AddressbookTB.ADDRESS_ADDRESSBOOD));
						
						addressbookList.add(addressbook);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return addressbookList;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public AddressbookTB getAddressbookById(Integer id){
		
		AddressbookTB addressbook = null;
		
		String sql ="SELECT * FROM " + AddressbookTB.TABLE_NAME + " WHERE " + AddressbookTB.ID_ADDRESSBOOD + " = " + id ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		

		try {
			if(resultSet.next()){
						addressbook = new AddressbookTB();
							
						addressbook.setId_addressbook(resultSet.getInt(AddressbookTB.ID_ADDRESSBOOD));
						addressbook.setName_addressbook(resultSet.getString(AddressbookTB.NAME_ADDRESSBOOD));
						addressbook.setCompany_addressbook(resultSet.getString(AddressbookTB.COMPANY_ADDRESSBOOD));
						addressbook.setDepartments_addressbook(resultSet.getString(AddressbookTB.DEPARTMENTS_ADDRESSBOOD));
						addressbook.setDes_addressbook(resultSet.getString(AddressbookTB.DES_ADDRESSBOOD));
						addressbook.setDuties_addressbook(resultSet.getString(AddressbookTB.DUTIES_ADDRESSBOOD));
						addressbook.setJobs_addressbook(resultSet.getString(AddressbookTB.JOBS_ADDRESSBOOD));
						addressbook.setKind_addressbook(resultSet.getString(AddressbookTB.KIND_ADDRESSBOOD));
						addressbook.setLable_addressbook(resultSet.getString(AddressbookTB.LABLE_ADDRESSBOOD));
						addressbook.setMobile_addressbook(resultSet.getString(AddressbookTB.MOBILE_ADDRESSBOOD));
						addressbook.setPhone_addressbook(resultSet.getString(AddressbookTB.PHONE_ADDRESSBOOD));
						addressbook.setQq_addressbook(resultSet.getString(AddressbookTB.QQ_ADDRESSBOOD));
						addressbook.setStatus_addressbook(resultSet.getString(AddressbookTB.STATUS_ADDRESSBOOD));
						addressbook.setAddress_addressbook(resultSet.getString(AddressbookTB.ADDRESS_ADDRESSBOOD));
				}
				resultSet.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
					
				
		
		
		return addressbook;
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<AddressbookTB> selectAddressbookByKind(String kind){
		String sql ="SELECT * FROM " + AddressbookTB.TABLE_NAME + " WHERE " + AddressbookTB.KIND_ADDRESSBOOD + " = '" + kind + "'";
		return selectAddressbookBySql(sql);
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean addAddressbook(AddressbookTB addressbook){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(AddressbookTB.COMPANY_ADDRESSBOOD, addressbook.getCompany_addressbook());
		insertMap.put(AddressbookTB.DEPARTMENTS_ADDRESSBOOD, addressbook.getDepartments_addressbook());
		insertMap.put(AddressbookTB.DES_ADDRESSBOOD, addressbook.getDes_addressbook());
		insertMap.put(AddressbookTB.DUTIES_ADDRESSBOOD, addressbook.getDuties_addressbook());
		insertMap.put(AddressbookTB.ADDRESS_ADDRESSBOOD, addressbook.getAddress_addressbook());
		
		insertMap.put(AddressbookTB.JOBS_ADDRESSBOOD, addressbook.getJobs_addressbook());
		insertMap.put(AddressbookTB.KIND_ADDRESSBOOD, addressbook.getKind_addressbook());
		insertMap.put(AddressbookTB.LABLE_ADDRESSBOOD, addressbook.getLable_addressbook());
		insertMap.put(AddressbookTB.MOBILE_ADDRESSBOOD, addressbook.getMobile_addressbook());
		insertMap.put(AddressbookTB.NAME_ADDRESSBOOD, addressbook.getName_addressbook());
		insertMap.put(AddressbookTB.PHONE_ADDRESSBOOD, addressbook.getPhone_addressbook());
		insertMap.put(AddressbookTB.QQ_ADDRESSBOOD, addressbook.getQq_addressbook());
		
		return sqlMachine.insertRow(AddressbookTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean modifyAddressbook(AddressbookTB addressbook,Integer id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();

		updateMap.put(AddressbookTB.COMPANY_ADDRESSBOOD, addressbook.getCompany_addressbook());
		updateMap.put(AddressbookTB.DEPARTMENTS_ADDRESSBOOD, addressbook.getDepartments_addressbook());
		updateMap.put(AddressbookTB.DES_ADDRESSBOOD, addressbook.getDes_addressbook());
		updateMap.put(AddressbookTB.DUTIES_ADDRESSBOOD, addressbook.getDuties_addressbook());
		updateMap.put(AddressbookTB.ADDRESS_ADDRESSBOOD, addressbook.getAddress_addressbook());
		
		updateMap.put(AddressbookTB.JOBS_ADDRESSBOOD, addressbook.getJobs_addressbook());
		updateMap.put(AddressbookTB.KIND_ADDRESSBOOD, addressbook.getKind_addressbook());
		updateMap.put(AddressbookTB.LABLE_ADDRESSBOOD, addressbook.getLable_addressbook());
		updateMap.put(AddressbookTB.MOBILE_ADDRESSBOOD, addressbook.getMobile_addressbook());
		updateMap.put(AddressbookTB.NAME_ADDRESSBOOD, addressbook.getName_addressbook());
		updateMap.put(AddressbookTB.PHONE_ADDRESSBOOD, addressbook.getPhone_addressbook());
		updateMap.put(AddressbookTB.QQ_ADDRESSBOOD, addressbook.getQq_addressbook());
		
		return sqlMachine.updateRow(AddressbookTB.TABLE_NAME, updateMap, AddressbookTB.ID_ADDRESSBOOD, id);
		
	}
}
